package com.senjoeson.baselib_ui.view;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.text.method.ScrollingMovementMethod;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.senjoeson.baselib_ui.R;

/**
 * @author sunqiao
 * @date 2018/7/5 11:54
 */

/**
 * 主要用于弹出通知类的自定义Dialog
 */
public class NoticeDialog extends Dialog implements View.OnClickListener {

    public TextView mNoticeTitle;
    public TextView mNoticeContent;

    public NoticeDialog(Context context) {
        super(context, R.style.DialogStyle);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_notice, null);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(view);
        setProperties();
        findViews(view);
    }

    /**
     * 给内部控件设置一些属性
     */
    private void findViews(View view) {
        mNoticeTitle = view.findViewById(R.id.notice_title);
        mNoticeContent = view.findViewById(R.id.notice_content);
        ImageView closeNotice = view.findViewById(R.id.close_notice_dialog);
        closeNotice.setOnClickListener(this);
        mNoticeContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        mNoticeContent.setScrollbarFadingEnabled(true);
        mNoticeContent.setVerticalScrollBarEnabled(true);
    }

    /**
     * 给dialog设置一些属性
     */
    public void setProperties() {
        //窗口,尺寸一些设置
        if (getWindow() != null) {
            getWindow().setGravity(Gravity.CENTER);
            getWindow().getDecorView().setPadding(40, 40, 40, 40);
            getWindow().setWindowAnimations(R.style.popupAnimation);
        }
        WindowManager windowManager = getWindow().getWindowManager();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        layoutParams.width = metrics.widthPixels;
        layoutParams.height = metrics.heightPixels;
        layoutParams.alpha = 1f;
        getWindow().setAttributes(layoutParams);

        //设置是否可取消弹框
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    public void setTitle(String title) {
        mNoticeTitle.setText(title);
    }

    public void setContent(String content) {
        mNoticeContent.setText(decodeString(content));
    }

    /**
     * 解析content内部html标签
     *
     * @param content 公告内容
     */
    public String decodeString(String content) {
        Spanned spanned = Html.fromHtml(content);
        return spanned.toString();
    }

    public static class Builder {
        private String title;

        private String content;

        private Context mContext;

        public Builder(Context context) {
            mContext = context;
        }

        public NoticeDialog.Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public NoticeDialog.Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public NoticeDialog build() {
            NoticeDialog noticeDialog = new NoticeDialog(mContext);
            noticeDialog.setTitle(title);
            noticeDialog.setContent(content);
            return noticeDialog;
        }
    }
}
